package com.scorpio.agentuser.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scorpio.agentuser.domain.AgentRegUserEntity;
import com.scorpio.agentuser.dto.RegUserProbablyDTO;
import com.scorpio.agentuser.enums.QueryDataEnum;
import com.scorpio.agentuser.mapper.AgentRegUserMapper;
import com.scorpio.agentuser.service.AgentRegUserService;
import com.scorpio.common.annotation.DataSource;
import com.scorpio.common.enums.DataSourceType;
import com.scorpio.common.exception.CustomException;
import com.scorpio.common.service.impl.BaseServiceImpl;
import com.scorpio.common.utils.EchartsUtil;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

@Service
@DataSource(DataSourceType.SLAVE)
public class AgentRegUserServiceImpl extends BaseServiceImpl<AgentRegUserMapper, AgentRegUserEntity> implements AgentRegUserService {


    @Override
    public RegUserProbablyDTO userProbably(Long agentId) {
        RegUserProbablyDTO dto = new RegUserProbablyDTO();

        //今日
        Calendar cal = Calendar.getInstance();
        int todatTotali = baseDao.countRegTotali(agentId,cal.getTime(), null, QueryDataEnum.day.getValue());


        //昨日
        cal.add(Calendar.DATE, -1);
        int yesterdayTotali = baseDao.countRegTotali(agentId,cal.getTime(), null,QueryDataEnum.day.getValue());

        //本周
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        int thisWeekTotali = baseDao.countRegTotaliByDateScope(agentId,cal.getTime(), new Date());

        //本月
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int thisMonthTotali = baseDao.countRegTotaliByDateScope(agentId,cal.getTime(), new Date());

        dto.setTodatTotali(todatTotali);
        dto.setYesterdayTotali(yesterdayTotali);
        dto.setThisWeekTotali(thisWeekTotali);
        dto.setThisMonthTotali(thisMonthTotali);
        return dto;
    }

    @Override
    public JSONObject statistics(Long agentId, Integer type, Date quertDate) {
        JSONObject option = new JSONObject();
        JSONArray flag = new JSONArray();
        JSONArray direct = new JSONArray();
        JSONArray indirect = new JSONArray();
        JSONArray totali = new JSONArray();

        Calendar cal = Calendar.getInstance();
        if (type == 1) {
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            for (int i = 0; i < 23; i++) {
                flag.add((i + 1) + "时");
                cal.add(Calendar.HOUR_OF_DAY, 1);
                direct.add(baseDao.countRegTotali(agentId,cal.getTime(), 1,QueryDataEnum.hour.getValue()));
                indirect.add(baseDao.countRegTotali(agentId,cal.getTime(), 2,QueryDataEnum.hour.getValue()));
            }
        } else if (type == 2 || type == 3) {
            if (type == 3) {
                if (quertDate == null) {
                    throw new CustomException("无效查询");
                }
                cal.setTime(quertDate);
            }
            cal.add(Calendar.MONTH, 0);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            int day = cal.getActualMaximum(Calendar.DATE);
            for (int i = 0; i < day; i++) {
                flag.add((i + 1) + "日");
                cal.add(Calendar.DAY_OF_MONTH, 1);
                direct.add(baseDao.countRegTotali(agentId,cal.getTime(), 1,QueryDataEnum.day.getValue()));
                indirect.add(baseDao.countRegTotali(agentId,cal.getTime(), 2,QueryDataEnum.day.getValue()));
            }
        } else {
            throw new CustomException("无效查询");
        }
        for (int i = 0; i < direct.size(); i++) {
            int t = direct.getInteger(i) + indirect.getInteger(i);
            totali.add(t);

        }
        option.put("tooltip", EchartsUtil.tooltip());
        option.put("toolbox", EchartsUtil.toolbox("line", "line"));
        option.put("legend", EchartsUtil.legend("直推", "间推", "总计"));

        option.put("xAxis", EchartsUtil.xAxis("category", flag, "shadow"));
        JSONArray yAxis = new JSONArray();
        int min = EchartsUtil.toAverageMin(totali);
        int max = EchartsUtil.toAverageMax(totali);
        yAxis.add(EchartsUtil.yAxisItem("value", "直推", min, max, max / 10, "{value} 人"));
        yAxis.add(EchartsUtil.yAxisItem("value", "间推", min, max, max / 10, "{value} 人"));

        JSONArray series = new JSONArray();
        series.add(EchartsUtil.seriesItem("直推", "bar", direct, null));
        series.add(EchartsUtil.seriesItem("间推", "bar", indirect, null));
        series.add(EchartsUtil.seriesItem("总计", "line", totali, 1));
        option.put("yAxis", yAxis);
        option.put("series", series);
        return option;
    }

    @Override
    public JSONObject statisticsPie(Long agentId, Integer type, Date quertDate) {
        JSONObject json = new JSONObject();
        JSONObject direct = new JSONObject();
        JSONObject indirect = new JSONObject();
        Calendar cal = Calendar.getInstance();
        if (type == 1) {
            Integer di =  baseDao.countRegTotali(agentId, cal.getTime(), 1, QueryDataEnum.day.getValue());
            Integer in = baseDao.countRegTotali(agentId, cal.getTime(), 2, QueryDataEnum.day.getValue());
            direct.put("value",di == null ? 0 : di);
            indirect.put("value",in == null ? 0 : in);
        } else if (type == 2 || type == 3) {
            if (type == 3) {
                if (quertDate == null) {
                    throw new CustomException("无效查询");
                }
                cal.setTime(quertDate);
            }
            Integer di = baseDao.countRegTotali(agentId, cal.getTime(), 1, QueryDataEnum.month.getValue());
            Integer in = baseDao.countRegTotali(agentId, cal.getTime(), 2, QueryDataEnum.month.getValue());
            direct.put("value",di == null ? 0 : di);
            indirect.put("value",in == null ? 0 : in);
        } else {
            throw new CustomException("无效查询");
        }
        direct.put("name", "直推注册");
        indirect.put("name", "间推注册");
        JSONArray data = new JSONArray();
        data.add(direct);
        data.add(indirect);
        JSONArray series = new JSONArray();
        series.add(EchartsUtil.seriesItem_Pie("注册人数", "55%", data));
        json.put("tooltip",EchartsUtil.tooltip_Pie());
        json.put("series",series);
        return json;
    }

    @Override
    public int countRegTotali(Long agentId, Date date, Integer recLevel, Integer dateLength) {
        return baseDao.countRegTotali(agentId,date,recLevel,dateLength);
    }
}
